﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Air_condition.DO;
using Air_condition.BU;
using Com.DA;
using System.Reflection;
using System.Media;


namespace Air_condition.GUI
{
    /// <summary>
    /// Interaction logic for TemplateManagement.xaml
    /// </summary>
    ///   

    public partial class TemplateManagement : Window
    {
        LibraryManagerInCom lbrManager = new LibraryManagerInCom();
        Room room1 = new Room();

        Glass glas = new Glass();
        Ceiling ceil = new Ceiling();
        Roof roof = new Roof();
        Wall wall = new Wall();
        Floor floor = new Floor();

        private InternalLoad inter = new InternalLoad();

        TemplateManager template = new TemplateManager();

        private int iRoom = 0;
        private int icountInter = 0;
        int icountCons = 0;
        public delegate void LoadTemplate(object o);
        public LoadTemplate MyTemplate;

        public TemplateManagement()
        {
            InitializeComponent();
            LoadSpace();
            LoadEquipment();
            LoadTypeOfGlass();
            LoadGlassDirection();
            LoadCeilling();
            LoadCeillingLayer();
            LoadWall();
            LoadRoofSurface();
            LoadStructureRoof();
            LoadCharacteristicRoof();
            LoadStructureFloor();
            LoadCharacteristicFloor();
            setData();
            btnApplyRoom.IsEnabled = false;
            btnApplyInternalLoad.IsEnabled = false;
            template.CreateDefault();
            Reloadroom();
            cmbDes_room.SelectedIndex = 0;
            ReloadcomboxInter();
            cbxdes_Internal.SelectedIndex = 0;
            ReloadcomboxConstruct();
            cbxDesConstr.SelectedIndex = 0;
        }

        #region Loading
        /// <summary>
        /// Set data for comboboxes
        /// </summary>
        public void setData()
        {
            // combobox Gender
            cbxGender.Items.Add("Male");
            cbxGender.Items.Add("Female");
            cbxGender.Items.Add("Child");

            //  combobox Latitue
            cbxLatitute.Items.Add("10 deg North");
            cbxLatitute.Items.Add("20 deg North");
            cbxLatitute.Items.Add("30 deg North");

            //  combobox Solar Hour
            for (int i = 6; i <= 17; i++)
            {
                cbxSolarHour.Items.Add(i);
            }

            //  combobox Month
            for (int i = 1; i <= 12; i++)
            {
                cbxMonth.Items.Add(i);
            }

            //  combobox Time
            for (int i = 1; i <= 24; i++)
            {
                cbxTime.Items.Add(i);
            }

            //  combobox Pressure
            cbxPressure.Items.Add(150);
            cbxPressure.Items.Add(500);
            cbxPressure.Items.Add(700);

            // combobox Color
            cbxColor.Items.Add("Dark Color");
            cbxColor.Items.Add("Light Color");
            cbxColor.Items.Add("Average Color");
            cbxColor.SelectedIndex = -1;
        }

        //---------------------------------------LOAD CÁC ĐỐI TƯỢNG--------------------------
        //-----------------------------------------------------------------------------------


        private void LoadSpace()
        {
            cbxSpaceConditioning.ItemsSource = null;
            cbxSpaceConditioning.Items.Clear();
            cbxSpaceConditioning.ItemsSource = LibraryManagerInCom.AirConditionPlaceList;
            cbxSpaceConditioning.DisplayMemberPath = "description";
        }

        /// <summary>
        /// Load data for combobox Type of Equipment
        /// </summary>
        public void LoadEquipment()
        {
            cbxTypeOfEquipment.ItemsSource = null;
            cbxTypeOfEquipment.Items.Clear();
            cbxTypeOfEquipment.ItemsSource = LibraryManagerInCom.EquipmentList;
            cbxTypeOfEquipment.DisplayMemberPath = "equipment_name";
        }

        /// <summary>
        /// Load data for combobox Type of Glass
        /// </summary>
        public void LoadTypeOfGlass()
        {
            cbxTypeOfGlass.ItemsSource = null;
            cbxTypeOfGlass.Items.Clear();
            cbxTypeOfGlass.ItemsSource = LibraryManagerInCom.GlassCoeffientList;
            cbxTypeOfGlass.DisplayMemberPath = "glass_type";
            cbxTypeOfGlass.SelectedIndex = -1;
        }

        /// <summary>
        /// Load data for combobox Direction
        /// </summary>
        public void LoadGlassDirection()
        {
            cbxDirection.ItemsSource = null;
            cbxDirection.Items.Clear();
            cbxDirection.ItemsSource = LibraryManagerInCom.GlassDirectionList;
            cbxDirection.DisplayMemberPath = "glass_direction";
            cbxDirection.SelectedIndex = -1;
        }

        /// <summary>
        /// Load data for combobox Type of Wall
        /// </summary>
        public void LoadWall()
        {
            cbxTypeofWall.ItemsSource = null;
            cbxTypeofWall.Items.Clear();
            cbxTypeofWall.ItemsSource = LibraryManagerInCom.WallMaterialList;
            cbxTypeofWall.DisplayMemberPath = "description";
        }

        /// <summary>
        /// Load data for combobox Structure of Ceilling
        /// </summary>
        public void LoadCeilling()
        {
            cbxStructureOfCeilling.ItemsSource = null;
            cbxStructureOfCeilling.Items.Clear();
            cbxStructureOfCeilling.ItemsSource = LibraryManagerInCom.CeilingList;
            cbxStructureOfCeilling.DisplayMemberPath = "description";
        }

        /// <summary>
        /// Load data for combobox Characteristics Of Ceilling
        /// </summary>
        public void LoadCeillingLayer()
        {
            cbxCharacteristicsOfCeilling.ItemsSource = null;
            cbxCharacteristicsOfCeilling.Items.Clear();
            cbxCharacteristicsOfCeilling.ItemsSource = LibraryManagerInCom.CeilingLayerList;
            cbxCharacteristicsOfCeilling.DisplayMemberPath = "layer_type";
        }

        /// <summary>
        /// Load data for combobox Structure of Floor
        /// </summary>
        public void LoadStructureFloor()
        {
            cbxStructureOfFloor.ItemsSource = null;
            cbxStructureOfFloor.Items.Clear();
            cbxStructureOfFloor.ItemsSource = LibraryManagerInCom.CeilingList;
            cbxStructureOfFloor.DisplayMemberPath = "description";
        }

        /// <summary>
        /// Load data for combobox Characteristics Of Floor
        /// </summary>
        public void LoadCharacteristicFloor()
        {
            cbxCharacteristicsOfFloor.ItemsSource = null;
            cbxCharacteristicsOfFloor.Items.Clear();
            cbxCharacteristicsOfFloor.ItemsSource = LibraryManagerInCom.CeilingLayerList;
            cbxCharacteristicsOfFloor.DisplayMemberPath = "layer_type";
        }

        /// <summary>
        /// Load data for combobox Structure of Roof
        /// </summary>
        public void LoadStructureRoof()
        {
            cbxStructureOfRoof.ItemsSource = null;
            cbxStructureOfRoof.Items.Clear();
            cbxStructureOfRoof.ItemsSource = LibraryManagerInCom.RoofCeilingStructureList;
            cbxStructureOfRoof.DisplayMemberPath = "roof_ceiling_structure";
        }

        /// <summary>
        /// Load data for combobox Characteristic of Roof
        /// </summary>
        public void LoadCharacteristicRoof()
        {
            cbxCharacteristicsOfRoof.ItemsSource = null;
            cbxCharacteristicsOfRoof.Items.Clear();
            cbxCharacteristicsOfRoof.ItemsSource = LibraryManagerInCom.RoofCeilingCharacteristicList;
            cbxCharacteristicsOfRoof.DisplayMemberPath = "roof_ceiling_characteristic";
        }

        /// <summary>
        /// Load data for combobox Type of roof surface
        /// </summary>
        public void LoadRoofSurface()
        {
            cbxTypeOfSurface.ItemsSource = null;
            cbxTypeOfSurface.Items.Clear();
            cbxTypeOfSurface.ItemsSource = LibraryManagerInCom.RoofSurfaceList;
            cbxTypeOfSurface.DisplayMemberPath = "surface_roof";
        }
        #endregion

        public void SendTemplate()
        {
            MyTemplate(template);
        }

        #region ROOM
        // su kien tao phong moi 
        private void btnNewRoom_Click(object sender, RoutedEventArgs e)
        {
            iRoom++;
            btnDeleteRoom.IsEnabled = true;
            btnApplyRoom.IsEnabled = true;
            btnCopyRoom.IsEnabled = true;
            CavasRoom.IsEnabled = true;

            Room room = new Room();
            bool overload = false;
            if (cmbDes_room.Text != "")
            {
                foreach (Room ro1 in template.lroom)
                {
                    if (cmbDes_room.Text == ro1.sName)
                    {
                        room.sName = "Room" + "-" + iRoom;
                        overload = true;
                        break;
                    }
                }
                if (!overload)
                {
                    room.sName = cmbDes_room.Text;
                }

            }
            if (cmbDes_room.Text == "")
            {
                room.sName = "Room" + "-" + iRoom;
            }
            CheckRoomName(room);
            template.AddNewRoom(room);
            Reloadroom();
            MyTemplate(template);
        }

        public void CheckRoomName(Room r)
        {
            foreach (Room ro in template.lroom)
            {
                if (r.sName == ro.sName)
                {
                    iRoom++;
                    r.sName = "Room-" + iRoom;
                    CheckRoomName(r);
                }
            }
        }

        // su kien xoa phong 
        private void btnDeleteRoom_Click(object sender, RoutedEventArgs e)
        {
            if (cmbDes_room.SelectedIndex != 0)
            {
                template.DeleteTemplateRoom((Room)cmbDes_room.SelectedItem);
                if (template.lroom.Count == 0)
                {
                    btnDeleteRoom.IsEnabled = false;
                    btnCopyRoom.IsEnabled = false;
                    btnApplyRoom.IsEnabled = false;
                    CavasRoom.IsEnabled = false;
                    iRoom = 0;
                }
                Reloadroom();
                MyTemplate(template);
            }
            else
            {
                MessageBox.Show("Cannot delete default template");
            }
        }

        //  tab room
        private void btnApplyRoom_Click(object sender, RoutedEventArgs e)
        {
            btnApplyRoom.IsEnabled = false;
            ApplyRoom();
            Reloadroom();
        }

        private void btnCopyRoom_Click(object sender, RoutedEventArgs e)
        {
            iRoom++;
            if (cmbDes_room.SelectedItem == null)
            {
                MessageBox.Show("Input paramenter, please");
            }
            else
            {
                Room r1 = new Room();
                template.CoppyTemplateRoom((Room)cmbDes_room.SelectedItem, r1);
            }
            Reloadroom();
            MyTemplate(template);
        }
        //  luu room  

        private void ApplyRoom()
        {
            if (room1 != null)
            {
                room1.sName = cmbDes_room.Text.ToString();
                room1.dbOutSideTemperature = Convert.ToDouble(txtOutsideTemperature.Text);
                room1.dbInsideTemperature = Convert.ToDouble(txtInsideTemperature.Text);
                room1.dbOutsideAirContainingLevel = Convert.ToDouble(txtOutsideAirContainingLevel.Text);
                room1.dbInsideAirContainingLevel = Convert.ToDouble(txtInsideAirContainingLevel.Text);
                room1.dbHeigh = Convert.ToDouble(txtHeighRoom.Text);
                room1.dbWidth = Convert.ToDouble(txtWidthRoom.Text);
                room1.dbLength = Convert.ToDouble(txtLengthRoom.Text);
                template.lroom[template.lroom.IndexOf(room1)] = room1;
                MyTemplate(template);
            }
        }

        //  Lam moi lai danh sach cau o combobox
        private void Reloadroom()
        {
            cmbDes_room.ItemsSource = null;
            cmbDes_room.Items.Clear();
            cmbDes_room.ItemsSource = template.lroom;
            cmbDes_room.DisplayMemberPath = "sName";
            cmbDes_room.SelectedIndex = 0;
        }


        //  chon lai mo ta 
        private void cmbDes_room_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((Room)cmbDes_room.SelectedItem != null)
            {
                room1 = (Room)cmbDes_room.SelectedItem;
                LoadtabRoom();
            }
        }

        //  Load thong so room kh nguoi dung chon lai 
        private void LoadtabRoom()
        {
            txtOutsideTemperature.Text = room1.dbOutSideTemperature.ToString();
            txtInsideAirContainingLevel.Text = room1.dbInsideAirContainingLevel.ToString();
            txtOutsideAirContainingLevel.Text = room1.dbOutsideAirContainingLevel.ToString();
            txtInsideTemperature.Text = room1.dbInsideTemperature.ToString();
            txtLengthRoom.Text = room1.dbLength.ToString();
            txtWidthRoom.Text = room1.dbWidth.ToString();
            txtHeighRoom.Text = room1.dbHeigh.ToString();
        }

        #endregion

        #region INTERNALLOAD TAB
        //  tao moi internalload
        private void btnNewInternalLoad_Click(object sender, RoutedEventArgs e)
        {
            icountInter++;
            btnApplyInternalLoad.IsEnabled = true;
            btnCopyInternalLoad.IsEnabled = true;
            btnDeleteInternalLoad.IsEnabled = true;
            CanvasInternal.IsEnabled = true;

            InternalLoad inter1 = new InternalLoad();
            bool overload = false;
            if (cbxdes_Internal.Text != "")
            {
                foreach (InternalLoad inter in template.linternalload)
                {
                    if (cbxdes_Internal.Text == inter.sName)
                    {
                        inter1.sName = "Internal" + "-" + icountInter;
                        overload = true;
                        break;
                    }
                }
                if (!overload)
                {
                    inter1.sName = cbxdes_Internal.Text;
                }
            }
            if (cbxdes_Internal.Text == "")
            {
                inter1.sName = "Internal" + "-" + icountInter;

            }
            Equip equip = new Equip();
            inter1.lequip.Add(equip);
            CheckInternalName(inter1);
            template.AddNewInternal(inter1);
            ReloadcomboxInter();
            MyTemplate(template);
        }

        public void CheckInternalName(InternalLoad i)
        {
            foreach (InternalLoad inter in template.linternalload)
            {
                if (i.sName == inter.sName)
                {
                    icountInter++;
                    i.sName = "Internal-" + icountInter;
                    CheckInternalName(i);
                }
            }
        }

        //  xoa internal load hien tai 
        private void btnDeleteInternalLoad_Click(object sender, RoutedEventArgs e)
        {
            if (cbxdes_Internal.SelectedIndex != 0)
            {
                template.DeleteTemplateInternal((InternalLoad)cbxdes_Internal.SelectedItem);
                if (template.linternalload.Count == 0)
                {
                    btnApplyInternalLoad.IsEnabled = false;
                    btnCopyInternalLoad.IsEnabled = false;
                    btnDeleteInternalLoad.IsEnabled = false;
                    icountInter = 0;
                    CanvasInternal.IsEnabled = false;
                }
                ReloadcomboxInter();
                MyTemplate(template);
            }
            else
            {
                MessageBox.Show("Cannot delete default template");
            }
        }
        //  sao chep internal load moi 
        private void btnCopyInternalLoad_Click(object sender, RoutedEventArgs e)
        {
            if (cbxdes_Internal.SelectedItem == null)
            {
                MessageBox.Show("Input paramenter, please");
            }
            else
            {
                InternalLoad coppyInternal = new InternalLoad();
                template.CoppyTemplateInternal((InternalLoad)(cbxdes_Internal.SelectedItem), coppyInternal);
            }
            ReloadcomboxInter();
            MyTemplate(template);
        }

        //  Load lai thong so combobox
        private void ReloadcomboxInter()
        {
            cbxdes_Internal.ItemsSource = null;
            cbxdes_Internal.Items.Clear();
            cbxdes_Internal.ItemsSource = template.linternalload;
            cbxdes_Internal.DisplayMemberPath = "sName";
            cbxdes_Internal.SelectedIndex = 0;
        }

        //  Load lai 1 doi tuong bat ki trong internal khi dc nguoi dung chon
        private void LoadformInternal(InternalLoad inter)
        {
            cbxSpaceConditioning.SelectedIndex = inter.iSpaceConditioning;
            txtPeopleDensity.Text = inter.dbDensityPeople.ToString();
            cbxGender.SelectedIndex = inter.iGender;
            txtAmountAir.Text = inter.dbAirMountForOnePeople.ToString();
            cbxTypeOfEquipment.SelectedIndex = inter.lequip[0].iType;
            txtEnergytOfEquipment.Text = inter.lequip[0].dbEnergy.ToString();
        }

        //  Apply internal load 
        private void btnApplyInternalLoad_Click(object sender, RoutedEventArgs e)
        {
            if (inter != null)
            {
                inter.sName = cbxdes_Internal.Text;
                inter.iSpaceConditioning = cbxSpaceConditioning.SelectedIndex;
                inter.dbDensityPeople = Convert.ToDouble(txtPeopleDensity.Text);
                inter.iGender = cbxGender.SelectedIndex;
                inter.dbAirMountForOnePeople = Convert.ToDouble(txtAmountAir.Text);
                inter.lequip[0].iType = cbxTypeOfEquipment.SelectedIndex;
                inter.lequip[0].dbEnergy = Convert.ToDouble(txtEnergytOfEquipment.Text);
                ReloadcomboxInter();
                MyTemplate(template);
                btnApplyInternalLoad.IsEnabled = false;
            }
        }


        private void cbxdes_Internal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((InternalLoad)cbxdes_Internal.SelectedItem != null)
            {
                inter = (InternalLoad)cbxdes_Internal.SelectedItem;
                LoadformInternal(inter);
            }
        }

        private void cbxTypeOfEquipment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_equipment equipment = (tb_equipment)cbxTypeOfEquipment.SelectedItem;
            if (equipment != null)
            {
                txtEnergytOfEquipment.Text = equipment.energy.ToString();
                btnApplyInternalLoad.IsEnabled = true;
            }
        }

        private void cbxSpaceConditioning_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_air_conditioning_place conplace = (tb_air_conditioning_place)cbxSpaceConditioning.SelectedItem;
            if (conplace != null)
            {
                txtPeopleDensity.Text = conplace.people_density.ToString();
                txtAmountAir.Text = conplace.air_amount_per_perople.ToString();
                btnApplyInternalLoad.IsEnabled = true;
            }
        }
        #endregion

        #region CONSTRUCTION
        //  Tạo mới construction
        private void btnNewConstruction_Click(object sender, RoutedEventArgs e)
        {
            icountCons++;
            Glass glas = new Glass();
            //   luu doi tuong Ceiling 
            Ceiling ceil = new Ceiling();
            //  luu doi tuong wall
            Wall wall = new Wall();
            //  luu doi tuong floor
            Floor floor = new Floor();
            bool overload = false;

            if (cbxDesConstr.Text != "")
            {
                foreach (Glass gl1 in template.lglass)
                {
                    if (cbxDesConstr.Text == gl1.sName)
                    {
                        glas.sName = "Construction" + "-" + icountCons;
                        overload = true;
                        break;
                    }
                }
                if (!overload)
                {
                    glas.sName = cbxDesConstr.Text.ToString().Trim();
                }
            }
            if (cbxDesConstr.Text == "")
            {
                glas.sName = "Construction" + "-" + icountCons;
            }
            CheckGlassName(glas);
            ceil.sName = glas.sName;
            floor.sName = glas.sName;
            wall.sName = glas.sName;
            template.AddnewConstruction(wall, floor, ceil, glas);
            ReloadcomboxConstruct();
            MyTemplate(template);
            btnDeleteConstruction.IsEnabled = true;
            btnCopyConstruction.IsEnabled = true;
            btnApplyConstruction.IsEnabled = true;
            CanvasCon.IsEnabled = true;
        }

        public void CheckGlassName(Glass g)
        {
            foreach (Glass gl in template.lglass)
            {
                if (g.sName == gl.sName)
                {
                    icountCons++;
                    g.sName = "Construction-" + icountCons;
                    CheckGlassName(g);
                }
            }
        }

        //  tao lai cac items trong hop mo ta
        private void ReloadcomboxConstruct()
        {
            cbxDesConstr.ItemsSource = null;
            cbxDesConstr.Items.Clear();
            cbxDesConstr.ItemsSource = template.lglass;
            cbxDesConstr.DisplayMemberPath = "sName";
            cbxDesConstr.SelectedIndex = 0;
        }

        private void cbxDesConstr_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((Glass)cbxDesConstr.SelectedItem != null)
            {
                glas = (Glass)cbxDesConstr.SelectedItem;
                roof = null;
                ceil = null;
                string name = glas.sName;
                roof = template.lroof.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                ceil = template.lceiling.SingleOrDefault(rk => rk.sName.Trim().Equals(name));
                wall = template.lwall.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                floor = template.lfloor.SingleOrDefault(rf => rf.sName.Trim().Equals(name));
                LoadConstruction();
            }
        }
        //  chon doi tuong trong construction 
        private void LoadConstruction()
        {
            cbxDesConstr.Text = glas.sName;
            cbxTypeOfFrameGlass.SelectedIndex = glas.iWindowFrameType;
            txtAreaOfDoor.Text = glas.dbArea.ToString();
            cbxTypeOfGlass.SelectedIndex = glas.iTypeofglass;
            txtCoefficientOfGlass.Text = glas.dbGlassCoefficient.ToString();
            cbxLatitute.SelectedIndex = glas.iLatitute;
            cbxSolarHour.SelectedIndex = glas.iSolarHour;
            cbxMonth.SelectedIndex = glas.iMonth;
            cbxPressure.SelectedIndex = glas.iPressure;
            cbxTime.SelectedIndex = glas.iTime;
            txtSolarRadiation.Text = glas.dbSolarRadition.ToString();
            cbxDirection.SelectedIndex = glas.iDirection;
            txtImmediate.Text = glas.dbImmediateCoefficient.ToString();

            if (ceil != null)
            {
                //   hien doi tuong Ceiling 
                radCeilling.IsChecked = false;
                radCeilling.IsChecked = true;
                txtLengthOfCeilling.Text = ceil.dbLength.ToString();
                txtWidthOfCeilling.Text = ceil.dbWidth.ToString();
                cbxCharacteristicsOfCeilling.SelectedIndex = ceil.iCharacteristics;
                cbxStructureOfCeilling.SelectedIndex = ceil.iStructure;
                txtHeatTransfer.Text = ceil.dbHeatTransferCoefficient.ToString();
            }
            else
            {
                //  hien doi tuong roof
                radRoof.IsChecked = false;
                radRoof.IsChecked = true;
                txtWidthOfRoof.Text = roof.dbWidth.ToString();
                txtLengthOfRoof.Text = roof.dbLength.ToString();
                cbxStructureOfRoof.SelectedIndex = roof.iStructure;
                cbxCharacteristicsOfRoof.SelectedIndex = roof.iCharacteristics;
                txtHeatTransferOfRoof.Text = roof.dbHeatTransferCoefficient.ToString();
                cbxColor.SelectedIndex = roof.iColor;
                cbxTypeOfSurface.SelectedIndex = roof.iSurface;
                txtAbsorptionCoefficient.Text = roof.dbAbsorptionCoefficient.ToString();
            }

            //  hien doi tuong wall
            txtWidthOfWall.Text = wall.dbWidth.ToString();
            txtLengthOfWall.Text = wall.dbLength.ToString();
            cbxTypeOfExposure.SelectedIndex = wall.iTypeExposure;
            cbxTypeofWall.SelectedIndex = wall.icharacteristics;
            txtHeatResistanceofWall.Text = wall.dbHeatResistance.ToString();

            //  hien doi tuong floor
            txtWidthOfFloor.Text = floor.dbWidth.ToString();
            txtLengthOfFloor.Text = floor.dbLength.ToString();
            cbxCharacteristicsOfFloor.SelectedIndex = floor.iCharacteristics;
            cbxStructureOfFloor.SelectedIndex = floor.iStructure;
            txtHeatTransferOfFloor.Text = floor.dbHeatTransferCoefficient.ToString();

        }

        private void btnApplyConstruction_Click(object sender, RoutedEventArgs e)
        {
            if (template.lglass.Count != 0 && glas != null)
            {
                ApplyConstruction();
                ReloadcomboxConstruct();
                btnApplyConstruction.IsEnabled = false;
            }
            else
            {
                MessageBox.Show("Input Paramener, please!");
            }
        }

        //
        private void ApplyConstruction()
        {
            //  
            glas.iWindowFrameType = cbxTypeOfFrameGlass.SelectedIndex;
            glas.sName = cbxDesConstr.Text.ToString();
            glas.dbArea = Convert.ToDouble(txtAreaOfDoor.Text);
            glas.iTypeofglass = cbxTypeOfGlass.SelectedIndex;
            glas.dbGlassCoefficient = Convert.ToDouble(txtCoefficientOfGlass.Text);
            glas.iLatitute = cbxLatitute.SelectedIndex;
            glas.iSolarHour = cbxSolarHour.SelectedIndex;
            glas.iMonth = cbxMonth.SelectedIndex;
            glas.iDirection = cbxDirection.SelectedIndex;
            glas.iPressure = cbxPressure.SelectedIndex;
            glas.iTime = cbxTime.SelectedIndex;
            glas.dbSolarRadition = Convert.ToDouble(txtSolarRadiation.Text);
            glas.dbImmediateCoefficient = Convert.ToDouble(txtImmediate.Text);
            //


            //
            if (radRoof.IsChecked == true)
            {
                Roof roof1 = new Roof();
                roof1.sName = cbxDesConstr.Text.ToString();
                roof1.dbWidth = Convert.ToDouble(txtWidthOfRoof.Text);
                roof1.dbLength = Convert.ToDouble(txtLengthOfRoof.Text);
                roof1.iStructure = cbxStructureOfRoof.SelectedIndex;
                roof1.iCharacteristics = cbxCharacteristicsOfRoof.SelectedIndex;
                roof1.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransferOfRoof.Text);
                roof1.iColor = cbxColor.SelectedIndex;
                roof1.iSurface = cbxTypeOfSurface.SelectedIndex;
                roof1.dbThermalRadiation = Convert.ToDouble(txtSolarRadiation.Text);
                roof1.dbAbsorptionCoefficient = Convert.ToDouble(txtAbsorptionCoefficient.Text);
                if (roof != null)
                {
                    template.lroof[template.lroof.IndexOf(roof)] = (Roof)roof1.Clone();
                }
                else
                {
                    template.lroof.Add(roof1);
                    template.lceiling.Remove(ceil);
                }
            }
            else
            {
                Ceiling ceil1 = new Ceiling();
                ceil1.sName = cbxDesConstr.Text.ToString();
                ceil1.dbWidth = Convert.ToDouble(txtWidthOfCeilling.Text);
                ceil1.dbLength = Convert.ToDouble(txtLengthOfCeilling.Text);
                ceil1.iStructure = cbxStructureOfCeilling.SelectedIndex;
                ceil1.iCharacteristics = cbxCharacteristicsOfCeilling.SelectedIndex;
                ceil1.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransfer.Text);
                if (ceil != null)
                {
                    template.lceiling[template.lceiling.IndexOf(ceil)] = (Ceiling)ceil1.Clone();
                }
                else
                {
                    template.lceiling.Add(ceil1);
                    template.lroof.Remove(roof);
                }
            }
            //-----------------------------------------------------------
            floor.sName = cbxDesConstr.Text.ToString();
            floor.dbWidth = Convert.ToDouble(txtWidthOfFloor.Text);
            floor.dbLength = Convert.ToDouble(txtLengthOfFloor.Text);
            floor.iStructure = cbxStructureOfFloor.SelectedIndex;
            floor.iCharacteristics = cbxCharacteristicsOfFloor.SelectedIndex;
            floor.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransferOfFloor.Text);

            //----------------------------------------
            wall.sName = cbxDesConstr.Text.ToString();
            wall.dbWidth = Convert.ToDouble(txtWidthOfWall.Text);
            wall.dbLength = Convert.ToDouble(txtLengthOfWall.Text);
            wall.iTypeExposure = cbxTypeOfExposure.SelectedIndex;
            wall.icharacteristics = cbxTypeofWall.SelectedIndex;
            wall.dbHeatResistance = Convert.ToDouble(txtHeatResistanceofWall.Text);
            //--------------------------------------------
            template.lwall[template.lwall.IndexOf(wall)] = wall;
            template.lfloor[template.lfloor.IndexOf(floor)] = floor;


            template.lglass[template.lglass.IndexOf(glas)] = glas;
            MyTemplate(template);

        }

        //  button copy/ 
        private void btnCopyConstruction_Click(object sender, RoutedEventArgs e)
        {
            if (template.lglass == null)
            {
                MessageBox.Show("Can't copy!");
            }
            else
            {
                icountCons++;
                template.CopyConstruction(((Glass)cbxDesConstr.SelectedItem).sName);
                ReloadcomboxConstruct();
                MyTemplate(template);
            }
        }
        //  button delete 
        private void btnDeleteConstruction_Click(object sender, RoutedEventArgs e)
        {
            if (cbxDesConstr.SelectedIndex != 0)
            {
                if (template.lglass != null)
                {
                    template.DeleteConstruction(((Glass)cbxDesConstr.SelectedItem).sName);
                    if (template.lglass.Count == 0)
                    {
                        CanvasCon.IsEnabled = false;
                        btnApplyConstruction.IsEnabled = false;
                        btnCopyConstruction.IsEnabled = false;
                        btnDeleteConstruction.IsEnabled = false;
                        icountCons = 0;
                    }
                    ReloadcomboxConstruct();
                    MyTemplate(template);
                }
            }
            else
            {
                MessageBox.Show("Cannot delete default template");
            }
        }

        //  Check RadioButon ceilling được chọn
        private void radCeilling_Checked(object sender, RoutedEventArgs e)
        {
            enableceiling(true);
            enableroof(false);
            btnApplyConstruction.IsEnabled = true;
        }

        private void radRoof_Checked(object sender, RoutedEventArgs e)
        {
            enableceiling(false);
            enableroof(true);
            btnApplyConstruction.IsEnabled = true;
        }

        private void enableroof(bool b)
        {
            txtWidthOfRoof.IsEnabled = b;
            txtLengthOfRoof.IsEnabled = b;
            cbxStructureOfRoof.IsEnabled = b;
            cbxCharacteristicsOfRoof.IsEnabled = b;
            txtHeatTransferOfRoof.IsEnabled = b;
            cbxColor.IsEnabled = b;
            cbxTypeOfSurface.IsEnabled = b;
            txtAbsorptionCoefficient.IsEnabled = b;
            lblRoof.IsEnabled = b;
            lblWidthOfRoof.IsEnabled = b;
            lblLengthOfRoof.IsEnabled = b;
            lblStructureOfRoof.IsEnabled = b;
            lblCharacteristicsOfRoof.IsEnabled = b;
            lblHeatTransferOfRoof.IsEnabled = b;
            lblColor.IsEnabled = b;
            lblTypeOfSurface.IsEnabled = b;
            lblAbsorptionCoefficient.IsEnabled = b;

        }
        private void enableceiling(bool b)
        {
            txtWidthOfCeilling.IsEnabled = b;
            txtLengthOfCeilling.IsEnabled = b;
            cbxStructureOfCeilling.IsEnabled = b;
            cbxCharacteristicsOfCeilling.IsEnabled = b;
            txtHeatTransfer.IsEnabled = b;
            lblCeiling.IsEnabled = b;
            lblWidthOfCeilling.IsEnabled = b;
            lblLengthOfCeilling.IsEnabled = b;
            lblStructureOfCeilling.IsEnabled = b;
            lblCharacteristicsOfCeilling.IsEnabled = b;
            lblHeatTransferOfCeilling.IsEnabled = b;
        }

        private void cbxTypeOfGlass_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxTypeOfGlass.SelectedItem != null)
            {
                txtCoefficientOfGlass.Text = ((tb_radiation_characteristics)cbxTypeOfGlass.SelectedItem).glass_coefficient.ToString();
                btnApplyConstruction.IsEnabled = true;
            }
        }

        private void cbxLatitute_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSolarRadition();
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxSolarHour_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSolarRadition();
            btnApplyConstruction.IsEnabled = true;
        }

        //  Được gọi lại 
        #region Load từng combo box khi được chọn
        private void loadSolarRadition()
        {
            if (cbxSolarHour.SelectedIndex != -1 && cbxDirection.SelectedIndex != -1 && cbxLatitute.SelectedIndex != -1 && cbxMonth.SelectedIndex != -1)
            {

                try
                {

                    int? strDirectioncbx = int.Parse(((tb_glass_direction)cbxDirection.SelectedItem).id.ToString());
                    int? strSolarHourcbx = (int)cbxSolarHour.SelectedItem;
                    int? strMonthcbx = (int)cbxMonth.SelectedItem; ;
                    string strLatitutecbx = cbxLatitute.SelectedItem.ToString();
                    tb_solar_radiation_through_glass ro = LibraryManagerInCom.SolarRadiationList.SingleOrDefault(r => r.solar_hour == strSolarHourcbx && r.direction_id == strDirectioncbx && r.month == strMonthcbx && r.latitute == strLatitutecbx);

                    if (ro != null)
                    {

                        txtSolarRadiation.Text = ro.radiation_amount.ToString();
                    }
                }
                catch (Exception)
                {
                    throw;

                }

            }
        }

        private void cbxMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSolarRadition();
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxDirection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSolarRadition();
            loadImmediateEffect();
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxPressure_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadImmediateEffect();
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxTime_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadImmediateEffect();
            btnApplyConstruction.IsEnabled = true;
        }

        private void loadImmediateEffect()
        {
            if (cbxDirection.SelectedIndex != -1 && cbxPressure.SelectedIndex != -1 && cbxTime.SelectedIndex != -1)
            {


                try
                {

                    int? Direction = int.Parse(((tb_glass_direction)cbxDirection.SelectedItem).id.ToString());
                    int? Pressure = (int)cbxPressure.SelectedItem;
                    int? Time = (int)cbxTime.SelectedItem;
                    tb_immediate_impact im = LibraryManagerInCom.ImmediateImpactList.SingleOrDefault(r => r.direction_id == Direction && r.time == Time && r.weight_per_meter_square == Pressure);
                    if (im != null)
                    {
                        txtImmediate.Text = im.coefficient.ToString();
                    }

                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        //

        #endregion


        private void cbxStructureOfCeilling_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadCeilling();
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxCharacteristicsOfCeilling_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadCeilling();
            btnApplyConstruction.IsEnabled = true;
        }

        private void loadCeilling()
        {
            if (cbxStructureOfCeilling.SelectedIndex != -1 && cbxCharacteristicsOfCeilling.SelectedIndex != -1)
            {
                tb_ceiling ce = (tb_ceiling)cbxStructureOfCeilling.SelectedItem;
                tb_ceiling_layer l = (tb_ceiling_layer)cbxCharacteristicsOfCeilling.SelectedItem;
                tb_heat_transfer_ceiling tr = LibraryManagerInCom.CeilingCoefficientList.SingleOrDefault(c => c.ceiling_id == ce.id && c.layer_id == l.id);
                if (tr != null)
                {
                    txtHeatTransfer.Text = tr.coefficient.ToString();
                }
            }
        }

        private void cbxStructureOfFloor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadFloor();
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxCharacteristicsOfFloor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadFloor();
            btnApplyConstruction.IsEnabled = true;
        }

        private void loadFloor()
        {
            if (cbxStructureOfFloor.SelectedIndex != -1 && cbxCharacteristicsOfFloor.SelectedIndex != -1)
            {
                tb_ceiling floor = (tb_ceiling)cbxStructureOfFloor.SelectedItem;
                tb_ceiling_layer layerfloor = (tb_ceiling_layer)cbxCharacteristicsOfFloor.SelectedItem;
                tb_heat_transfer_ceiling tran = LibraryManagerInCom.CeilingCoefficientList.SingleOrDefault(c => c.ceiling_id == floor.id && c.layer_id == layerfloor.id);
                if (tran != null)
                {
                    txtHeatTransferOfFloor.Text = tran.coefficient.ToString();
                }
            }
        }

        private void cbxTypeofWall_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxTypeofWall.SelectedItem != null)
            {
                txtHeatResistanceofWall.Text = ((tb_heat_resistance)cbxTypeofWall.SelectedItem).heat_resistance.ToString();
                btnApplyConstruction.IsEnabled = true;
            }
        }

        private void cbxStructureOfRoof_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadRoof();
            btnApplyConstruction.IsEnabled = true;
        }


        private void cbxCharacteristicsOfRoof_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadRoof();
            btnApplyConstruction.IsEnabled = true;
        }

        private void loadRoof()
        {
            if (cbxStructureOfRoof.SelectedIndex != -1 && cbxCharacteristicsOfRoof.SelectedIndex != -1)
            {
                tb_roof_ceiling_structure structureroof = (tb_roof_ceiling_structure)cbxStructureOfRoof.SelectedItem;
                tb_roof_ceiling_characteristic characroof = (tb_roof_ceiling_characteristic)cbxCharacteristicsOfRoof.SelectedItem;
                tb_roof_ceiling_heat_coefficient ro = LibraryManagerInCom.RoofCeilingHeatList.SingleOrDefault(r => r.structure_id == structureroof.id && r.characteristic_id == characroof.id);
                if (ro != null)
                {
                    txtHeatTransferOfRoof.Text = ro.heat_coefficient.ToString();
                }
            }
        }

        private void cbxTypeOfSurface_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxTypeOfSurface.SelectedItem != null)
            {
                txtAbsorptionCoefficient.Text = ((tb_roof_surface)cbxTypeOfSurface.SelectedItem).absorption_coefficient.ToString();
                btnApplyConstruction.IsEnabled = true;
            }
        }
        #endregion

        private void frmTemplateMagager_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }

        private void cbxTypeOfFrameGlass_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtHeatResistanceofWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxTypeOfExposure_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyConstruction.IsEnabled = true;
        }

        private void cbxColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyConstruction.IsEnabled = true;
        }
        #endregion

        #region EMPTY VALUE

        public void CheckEmpty(TextBox txt)
        {
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #region for room
        // Tab room

        private void cmbDes_room_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (template.lroom.Count != 0)
            {
                btnApplyRoom.IsEnabled = true;
            }
        }

        private void txtWidthRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthRoom);
            btnApplyRoom.IsEnabled = true;
        }

        private void txtHeighRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeighRoom);
            btnApplyRoom.IsEnabled = true;
        }

        private void txtLengthRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthRoom);
            btnApplyRoom.IsEnabled = true;
        }


        private void txtOutsideTemperature_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtOutsideTemperature);
            btnApplyRoom.IsEnabled = true;
        }


        private void txtOutsideAirContainingLevel_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtOutsideAirContainingLevel);
            btnApplyRoom.IsEnabled = true;
        }

        private void txtInsideAirContainingLevel_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtInsideAirContainingLevel);
            btnApplyRoom.IsEnabled = true;
        }

        private void txtInsideTemperature_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtInsideTemperature);
            btnApplyRoom.IsEnabled = true;
        }
        #endregion

        #region for internal
        private void cbxdes_Internal_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (template.linternalload.Count != 0)
            {
                btnApplyInternalLoad.IsEnabled = true;
            }
        }

        private void txtNumberOfPeople_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtPeopleDensity);
            btnApplyInternalLoad.IsEnabled = true;
        }

        private void txtAmountAir_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtAmountAir);
            btnApplyInternalLoad.IsEnabled = true;
        }

        private void cbxGender_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnApplyInternalLoad.IsEnabled = true;
        }
        #endregion

        #region for construction
        private void txtAreaOfDoor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtAreaOfDoor);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtCoefficientOfGlass_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtCoefficientOfGlass);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtSolarRadiation_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtSolarRadiation);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtImmediate_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtImmediate);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtWidthOfCeilling_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfCeilling);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtHeighOfCeilling_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthOfCeilling);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtHeatTransfer_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeatTransfer);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtWidthOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfRoof);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtLengthOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthOfRoof);
            btnApplyConstruction.IsEnabled = true;
        }


        private void txtAbsorptionCoefficient_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtAbsorptionCoefficient);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtHeatTransferOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeatTransferOfRoof);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtWidthOfWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfWall);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtLengthOfWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthOfWall);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtWidthOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfFloor);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtLengthOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthOfFloor);
            btnApplyConstruction.IsEnabled = true;
        }

        private void txtHeatTransferOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeatTransferOfFloor);
            btnApplyConstruction.IsEnabled = true;
        }
        private void txtEnergytOfEquipment_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtEnergytOfEquipment);
            btnApplyInternalLoad.IsEnabled = true;
        }
        #endregion

        private void cbxDesConstr_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (template.lglass.Count != 0)
            {
                btnApplyConstruction.IsEnabled = true;
            }
        }

        #endregion



    }
}




